const path = require("path");

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  publicPath: "./",
  outputDir: "dist",
  assetsDir: "static",
  indexPath: "index.html",
  filenameHashing: true,
  lintOnSave: process.env.NODE_ENV !== "production",
  productionSourceMap: false,
  pwa: {
    iconPaths: {
      favicon32: "favicon.ico",
      favicon16: "favicon.ico",
      appleTouchIcon: "favicon.ico",
      maskIcon: "favicon.ico",
      msTileImage: "favicon.ico",
    },
  },
  configureWebpack: (config) => {
    if (process.env.NODE_ENV === "production") {
      config.mode = "production";

      let optimization = {
        runtimeChunk: "single",
        splitChunks: {
          chunks: "all",
          maxInitialRequests: Infinity,
          minSize: 20000,
          cacheGroups: {
            vendor: {
              test: /[\\/]node_modules[\\/]/,
              name(module) {
                const packageName = module.context.match(
                  /[\\/]node_modules[\\/](.*?)([\\/]|$)/
                )[1];
                return `npm.${packageName.replace("@", "")}`;
              },
            },
          },
        },
      };

      Object.assign(config, {
        optimization,
      });
    } else {
      config.mode = "development";
    }

    config.resolve.alias = {
      "@": resolve("src"),
      common: resolve("src/components/common"),
      page: resolve("src/components/page"),
      account: resolve("src/components/page/account"),
      dashboard: resolve("src/components/page/dashboard"),
      detailsPage: resolve("src/components/page/detailsPage"),
      historyData: resolve("src/components/page/historyData"),
      management: resolve("src/components/page/management"),
      warning: resolve("src/components/page/warning"),
      itemMonitorCom: resolve("src/components/page/ItemMonitor/components"),
    };
  },
  devServer: {
    open: false,
    disableHostCheck: true,
    proxy: {
      "/api1": {
        target: "http://172.20.1.137:8080",
        changeOrigin: true,
        pathRewrite: {
          "^/api1": "",
        },
      },
      "/api2": {
        target: "http://172.20.1.137:7474",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api2": "",
        },
      },
    },
  },
};
